package com.gxuwz.web.struts2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import com.github.abel533.echarts.Option;
import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.Magic;
import com.github.abel533.echarts.code.MarkType;
import com.github.abel533.echarts.code.Tool;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.data.PointData;
import com.github.abel533.echarts.feature.MagicType;
import com.github.abel533.echarts.series.Bar;
import com.google.gson.Gson;
import com.gxuwz.common.Const;
import com.gxuwz.entity.SysLaboratory;
import com.gxuwz.entity.SysRepairApplication;
import com.gxuwz.service.ILaboratoryService;
import com.gxuwz.service.IRepairApplicationService;
import com.gxuwz.web.AbstractBaseController;

/**
 * 图表控制器
 * 
 * @author 小胡
 * @date 2017年6月8日
 */
public class ChartController extends AbstractBaseController {

	private static final long serialVersionUID = 165092635874017917L;

	// 图表常量
	public final static String CHART_TITLE = "设备检测情况";
	public final static String CHART_SUB_TITLE = "纯属虚构";
	public final static String MAX_VALUE = "最大值";
	public final static String MIN_VALUE = "最小值";
	public final static String AVERAGE_VALUE = "平均值";

	@Resource(name = "repairApplicationService")
	private IRepairApplicationService repairApplicationService;
	@Resource(name = "laboratoryService")
	private ILaboratoryService laboratoryService;

	private String result; // 获取图表的JSON数据

	/**
	 * 创建图表
	 * 
	 * @return
	 */
	public String createChart() {
		// 地址：http://echarts.baidu.com/doc/example/bar1.html
		Option option = new Option();
		option.title().text(CHART_TITLE).subtext(CHART_SUB_TITLE);
		option.tooltip().trigger(Trigger.axis);
		option.legend(Const.EXAMINED, Const.NO_EXAMINE);
		option.toolbox()
				.show(true)
				.feature(Tool.mark, Tool.dataView,
						new MagicType(Magic.line, Magic.bar).show(true),
						Tool.restore, Tool.saveAsImage);
		option.calculable(true);

		Map<String, Integer> notDetectMap = getNotDetectApply();
		Map<String, Integer> detectMap = getDetectApply();

		String[] labNames = getAllLabName();
		// 设置横坐标
		option.xAxis(new CategoryAxis().data(labNames));

		option.yAxis(new ValueAxis());
		List<Integer> detectValue = new ArrayList<>();
		Bar bar = new Bar(Const.EXAMINED);
		for (int i = 0; i < labNames.length; i++) {
			// 遍历map中的值
			for (Entry<String, Integer> m : detectMap.entrySet()) {
				if (m.getKey().equals(labNames[i])) {
					detectValue.add(m.getValue());
				}
			}
		}
		bar.data(detectValue.toArray());
		bar.markPoint().data(
				new PointData().type(MarkType.max).name(MAX_VALUE),
				new PointData().type(MarkType.min).name(MIN_VALUE));
		bar.markLine().data(
				new PointData().type(MarkType.average).name(AVERAGE_VALUE));
		Bar bar2 = new Bar(Const.NO_EXAMINE);

		List<Integer> notDetectValue = new ArrayList<Integer>();

		for (int i = 0; i < labNames.length; i++) {
			// 遍历map中的值
			for (Entry<String, Integer> m : notDetectMap.entrySet()) {
				if (m.getKey().equals(labNames[i]))
					notDetectValue.add(m.getValue());

			}
		}

		bar2.data(notDetectValue.toArray());
		bar2.markPoint().data(
				new PointData().type(MarkType.max).name(MAX_VALUE),
				new PointData().type(MarkType.min).name(MIN_VALUE));
		bar2.markLine().data(
				new PointData().type(MarkType.average).name(AVERAGE_VALUE));
		option.series(bar, bar2);

		Gson gson = new Gson();
		result = gson.toJson(option);
		return SUCCESS;
	}

	/**
	 * 获取未检测审批表
	 * 
	 * @date 2017年6月9日
	 */
	public Map<String, Integer> getNotDetectApply() {
		return getApplyByStatus(Const.NO_EXAMINE);
	}

	/**
	 * 获取已检测审批表
	 * 
	 * @date 2017年6月9日
	 */
	public Map<String, Integer> getDetectApply() {
		return getApplyByStatus(Const.EXAMINED);
	}

	/**
	 * 根据状态获取申请表
	 * 
	 * @param status
	 * @return
	 */
	public Map<String, Integer> getApplyByStatus(String status) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		final String hql = "from SysRepairApplication where status = ?";
		// 查询未检测的申请表
		List<SysRepairApplication> applyList = repairApplicationService.list(
				hql, status);
		for (int i = 0; i < applyList.size(); i++) {
			String labName = applyList.get(i).getLabName();
			if (map.containsKey(labName)) {
				int val = map.get(labName) + 1; // 如果已经包含则数量+1
				map.put(labName, val); // 将+1后的值存放回map
			} else {
				// 如果未包含则初始化为1
				map.put(labName, 1);
			}
		}
		return map;
	}

	/**
	 * 把所有的实验室数据转成字符数组
	 * 
	 * @return
	 */
	public String[] getAllLabName() {
		List<SysLaboratory> applyList = laboratoryService.getAllFromTable();
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < applyList.size(); i++) {
			list.add(applyList.get(i).getLabName());
		}
		String[] strings = new String[list.size()];
		return list.toArray(strings);
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
}
